package indi.yangshenhui.tool;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);

	/**
	 * 最优单例模式
	 */
	private PropertiesUtil() {
	}

	private static class SingletonHolder {
		private static final PropertiesUtil INSTANCE = new PropertiesUtil();

		private SingletonHolder() {
		}
	}

	public static PropertiesUtil getInstance() {
		return SingletonHolder.INSTANCE;
	}

	/**
	 * 只能读取classPath下配置文件
	 * 
	 * @param file
	 * @return Properties
	 */
	public Properties getProperties(String file) {
		Properties properties = new Properties();
		InputStream inputStream = null;
		try {
			inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(file);
			properties.load(inputStream);
		} catch (IOException e) {
			LOGGER.error(e.getMessage(), e);
		} finally {
			CloseUtil.close(inputStream);
		}
		return properties;
	}

}